<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * class School_model
 * @author Davinder
 * @version 1.0
 */
 
class School_model extends CI_Model
{
    private $_tables;
	private $_debug_mode = FALSE;
	const PAGE_SIZE = 100;
	
	public function __construct() {
        parent::__construct();
		
		$this->_tables = new stdClass();
        $this->_tables->schools = 'yb_scrapm_schools';
        $this->_tables->states = 'yb_scrapm_states';
        $this->_tables->profiles = 'yb_scrapm_profiles';
		
		// Load DB if its enabled
		if ( config_item('rest_database_group') )
		{
			$this->db = $this->load->database(config_item('rest_database_group'), TRUE);
		}
	}
    
    public function get_active_schools($p_data=NULL) {
        
        $school_state = $p_data->school_state;
        $school_city = $p_data->school_city;
        $school_name = $p_data->school_name;
        
        $this->db->where('school_is_active',1);
        $this->db->where('school_state',$school_state);
        $this->db->like('school_city',$school_city,'none');
        $this->db->like('school_name',$school_name,'after');
        $this->db->from($this->_tables->schools);
        $this->db->join($this->_tables->states,'yb_scrapm_schools.school_state = yb_scrapm_states.state_id','LEFT');
        $query = $this->db->get();
		
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
        
            $data = array();
            
            $i = 0;
            $resultSet = $query->result();
            
			foreach ( $resultSet as $row ) {
                
				$data['resultSet'][$i] = new stdClass();
                ////$data['resultSet'][$i]->idn = ($i+1);
                $data['resultSet'][$i]->id = $row->school_id;
                $data['resultSet'][$i]->name = $row->school_name;
                $data['resultSet'][$i]->address = $row->school_address;
                $data['resultSet'][$i]->city = $row->school_city;
                $data['resultSet'][$i]->state = $row->school_state;
				$data['resultSet'][$i]->state_name = $row->state_name;
				
				$data['resultSet'][$i]->district = $row->school_district;
				$data['resultSet'][$i]->phone = $row->school_phone;
				$data['resultSet'][$i]->county = $row->school_county;
				$data['resultSet'][$i]->geocode_lat = $row->school_geocode_lat;
				$data['resultSet'][$i]->geocode_long = $row->school_geocode_long;
				$data['resultSet'][$i]->zipcode = $row->school_zipcode;
				$data['resultSet'][$i]->lowest_grade = $row->school_lowest_grade;
				$data['resultSet'][$i]->highest_grade = $row->school_highest_grade;
				
				/*school_type
				school_religious_affiliation
				school_operational_status
				school_has_library
				school_gender_type
				school_academic_type
				school_academic_level*/
                
                $i++;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_states($p_data=null) {
        
        $state_name = $p_data->school_state;
        
        $this->db->like('state_name',$state_name,'after');
        $query = $this->db->get($this->_tables->states);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }
        
        $data = array();

        if ( $query->num_rows() > 0 ) {
            
            $i = 0;
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
  
                $data['resultSet'][$i] = new stdClass();
                
                $data['resultSet'][$i]->id = $row->state_id;
                $data['resultSet'][$i]->name = $row->state_name;
                $data['resultSet'][$i]->code = $row->state_code;
                
                $i++;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_cities($p_data=null) {
        
        $school_state = $p_data->school_state;
        $school_city = $p_data->school_city;
        
        $this->db->where('school_state',$school_state);
        $this->db->like('school_city',$school_city,'after');
        $this->db->from($this->_tables->schools);
        $this->db->join($this->_tables->states,'yb_scrapm_schools.school_state = yb_scrapm_states.state_id','LEFT');
        $this->db->distinct();
        $this->db->select("school_city");
        $query = $this->db->get();
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
  
                $data['resultSet'][] = $row->school_city;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
    
    public function get_users_by_school($p_school_id) {
        
        if ( empty($p_school_id) ) {
            return FALSE;
        }
        
        $this->db->select('profile_user_id');
        $this->db->where('profile_school_id',$p_school_id);
        $this->db->distinct();
        $query = $this->db->get($this->_tables->profiles);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {
                
                if ( !empty($row->profile_user_id) ) {
                    $data['resultSet'][] = $row->profile_user_id;
                }
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
}